//
//  TabBarVc.m
//  new
//
//  Created by going on 16/3/15.
//  Copyright © 2016年 going_Shui. All rights reserved.
//

#import "TabBarVc.h"
#import "MainViewController.h"
#import "FindPersonViewController.h"
#import "BuildServerViewController.h"
#import "MyCenterViewController.h"
#import "TMHNavigationController.h"



@interface TabBarVc ()<UITabBarControllerDelegate,UIAlertViewDelegate>

{
//    TabBarVc * _tabBarVc;
      UIView *_viewBackgroup;
}
@end

@implementation TabBarVc


- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    isUser
    if (user.user_id) {
        [self getDataId:user.user_id andToken:user.token];
    }
    [self configTabBar];
    [self creatViewControllers];
    
    

}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    isUser
    if (user.user_id) {
        [self getDataId:user.user_id andToken:user.token];
    }
    if (viewController ==self.viewControllers[1]) {
        [_viewBackgroup removeFromSuperview];
        if (!user.user_id){
            SHuiMBhudTextTimeStr(@"请先登录");
            return NO;
        }
        if ([user.user_type isEqualToString:@"user"]){
                SHuiMBhudTextTimeStr(@"未认证公司信息，无操作权限");
                return NO;
        }
    }
    
    if (viewController ==self.viewControllers[2]) {
        USER_DEFAULTSet(@"project", @"server");
        [_viewBackgroup removeFromSuperview];
        [self serverUI];
        
    }
    
    if (viewController ==self.viewControllers[0]) {
        
        [_viewBackgroup removeFromSuperview];
        
    }
    
    if (viewController ==self.viewControllers[3]) {
        
        [_viewBackgroup removeFromSuperview];
        
    }
    
    return YES;
}

-(void)configTabBar {
    self.tabBar.translucent = NO;
}

-(void)creatViewControllers {
    
    MainViewController * homeVC = [[MainViewController alloc]init];
    [self addChildViewController:homeVC title:@"找工作"imageName:@"icon_btn_home_jops_pre" selectedImageName:@"icon_btn_home_jops"];
   
    FindPersonViewController * findVC = [[FindPersonViewController alloc]init];
    [self addChildViewController:findVC title:@"招人才" imageName:@"icon_btn_home_recruit_pre" selectedImageName:@"icon_btn_home_recruit"];
    
    BuildServerViewController * server = [[BuildServerViewController alloc]init];
    [self addChildViewController:server title:@"建企宝" imageName:@"icon_btn_home_service_pre" selectedImageName:@"icon_btn_home_service"];

    MyCenterViewController   * myCenterVC = [[MyCenterViewController alloc]init];
    [self addChildViewController:myCenterVC title:@"我的" imageName:@"icon_btn_home_user_pre" selectedImageName:@"icon_btn_home_user"];
}
/**
 *  封装添加vc的方法
 */
- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    childController.title = title;
    childController.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 
    TMHNavigationController *nav = [[TMHNavigationController alloc] initWithRootViewController:childController];
 
    [self addChildViewController:nav];
    
    NSDictionary * detdic =@{NSForegroundColorAttributeName:[UIColor lightGrayColor]};
    [nav.tabBarItem setTitleTextAttributes:detdic forState:UIControlStateNormal];
    //改变颜色
    NSDictionary * seledic =@{NSForegroundColorAttributeName:RGB(0x00b7ee)};
    [nav.tabBarItem setTitleTextAttributes:seledic forState:UIControlStateSelected];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 获取数据
- (void)getDataId:(NSString*)userId andToken:(NSString *)token{
    
    NSString *strUrl = [NSString stringWithFormat:get_UserById_api];
    NSDictionary *parameter = @{@"user_id":userId, @"token":token};
    [PostNetWorking postNetWorkingStr:strUrl andparameters:parameter Success:^(id responseObject) {
        int code = [responseObject[@"code"]intValue];
        switch (code) {
            case 200:
            {
                // 存储用户信息
                [UserModel responseObject:responseObject];
            }
                break;
            case 220:
            {
                SHuiMBhudTextTimeStr(@"登录过期,重新登录");
                [UserManager clearUserInfo];
            }
                break;
            default:
                break;
        }
        
    } failed:^(NSError *error) {
        [MBProgressHUD showMessag:@"网络错误" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
    }];
}

#pragma mark -- 建企服务
-(void)serverUI{
    
    _viewBackgroup = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-49)];
    _viewBackgroup.backgroundColor = [UIColor colorWithColor:[UIColor blackColor] alpha:0.5];
    UIWindow *window = [self lastWindow];
    [window addSubview:_viewBackgroup];
    
    UIView *viewButton = [[UIView alloc] initWithFrame:CGRectMake(30, HEIGHT/2-150/2, WIDTH-60, 150)];
    viewButton.backgroundColor = [UIColor whiteColor];
    viewButton.layer.cornerRadius = 5;
    viewButton.layer.masksToBounds = YES;
    [_viewBackgroup addSubview:viewButton];
    
    NSArray *arrIcon = @[@"icon_service_gong",@"icon_service_qiu"];
//    NSArray *arrTitle = @[@"找项目",@"找团队"];
    
    for (int i = 0; i <2; i ++) {
        UIImage *image = [UIImage imageNamed:arrIcon[i]];
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(viewButton.frame.size.width/2*i, 0, viewButton.frame.size.width/2+15, viewButton.frame.size.height)];
        [button setImage:image forState:UIControlStateNormal];
//        [button setTitle:arrTitle[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button setTitleColor:strColor(@"333") forState:UIControlStateNormal];
        button.backgroundColor = [UIColor whiteColor];
        [button addTarget:self action:@selector(buttonOnClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 700+i;
        [viewButton addSubview:button];
//        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:16]};
//        CGSize size1 = [arrTitle[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, button.titleLabel.frame.size.height) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
        
//        button.titleEdgeInsets =UIEdgeInsetsMake(0.5*image.size.height+5, -0.5*image.size.width, -0.5*image.size.height, 0.5*image.size.width);
//        button.imageEdgeInsets =UIEdgeInsetsMake(-0.5*size1.height-5, 0.5*size1.width, 0.5*size1.height, -0.5*size1.width);
    }
    
}

-(void)buttonOnClick:(UIButton *)sender{
    [_viewBackgroup removeFromSuperview];
    
    if (sender.tag == 700) {
        USER_DEFAULTSet(@"project", @"server");
        
    }
    
    if (sender.tag == 701) {
        USER_DEFAULTSet(@"team", @"server");
    }
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"server" object:nil userInfo:@{@"server":@"1"}];
}


- (UIWindow *)lastWindow
{
    NSArray *windows = [UIApplication sharedApplication].windows;
    for(UIWindow *window in [windows reverseObjectEnumerator]) {
        if ([window isKindOfClass:[UIWindow class]] &&
            CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds))
            return window;
    }
    return [UIApplication sharedApplication].keyWindow;
}




@end
